package com.qf.ebook.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ebook.entity.EbookEntry;
import com.qf.ebook.mapper.EbookEntryMapper;
import com.qf.ebook.service.EbookEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EbookEntryServiceImpl implements EbookEntryService {

    @Autowired
    private EbookEntryMapper ebookEntryMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.insertSelective(ebookEntry);
        if(i!=1){
            System.out.println("<添加图书信息> 添加失败!!!");
            throw new RuntimeException("\"<添加图书信息> 添加失败!!!\"");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(EbookEntry ebookEntry) {
        int i = ebookEntryMapper.updateByPrimaryKeySelective(ebookEntry);
        if(i!=1){
            System.out.println("<修改图书信息> 修改失败!!!");
            throw new RuntimeException("\"<修改图书信息> 修改失败!!!\"");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Integer id) {
        int i = ebookEntryMapper.deleteByPrimaryKey((long)id);
        if(i!=1){
            System.out.println("<删除图书信息> 删除失败!!!");
            throw new RuntimeException("\"<删除图书信息> 删除失败!!!\"");
        }
    }

    @Override
    public PageInfo<EbookEntry> findByCategoryIdAndPage(Integer page, Integer rows, Integer categoryId) {
        //1、分页
        PageHelper.startPage(page, rows);
        //2、查询
        List<EbookEntry> ebookEntryList=ebookEntryMapper.findByCategoryId(categoryId);
        //3、封装PageInfo
        PageInfo<EbookEntry> pageInfo=new PageInfo<>(ebookEntryList);
        //4、返回
        return pageInfo;
    }
}
